<?php
namespace app\common\service;;

use app\common\logic\OrderLogic;
use think\facade\Db;

/**
 * Class MyOrder
 * @package app\common\service
 * 我的订单
 */
class Peisong extends Base {

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单列表数据
     */
    public function orderlist(){
        $page = input('pagenum');
        //列表过滤器，生成查询Map对象
        $map = [];
        $map['ps_user_id'] = $this->user_id;
        $status = input('st')?:'0';
        if($status==3){
            $map['peisong_status'] = ['in',[3,4]];
        }elseif($status!='all'){
            $map['peisong_status'] = $status;
        }
        //查询订单列表
        $field = 'order_id,order_sn,order_status,order_status,total_shop_price,s_user_id,shipping_name,delivery_no,sid,refund_id';
        $lists = Db::name('order')
            ->where($map)
            ->order('order_id desc')
            ->field($field)
            ->paginate(['page'=>$page,'list_rows'=>15],false);
        if($lists->total()){
            foreach($lists as $k=>$v){
                $goods_list = Db::name('order_goods')
                    ->alias('og')
                    ->join('goods g','og.goods_id=g.goods_id')
                    ->where(['order_id'=>$v['order_id']])
                    ->field('og.goods_id,og.goods_name,og.goods_num,og.shop_price,og.spec_key_name,g.original_img')
                    ->select()->toArray();
                $num_total = 0;
                if($goods_list){
                    foreach($goods_list as $kk=>$vv){
                        $goods_list[$kk]['image'] = $vv['original_img'];
                        $num_total = bcadd($num_total,$vv['goods_num']);
                    }
                }
                $v['num_total'] = (int)$num_total;
                $v['goods_list'] = $goods_list;
                if($v['sid']>0){
                    $supply_info = Db::name('suppliers')->where('id',$v['sid'])->find();
                }
                if(!$supply_info){
                    $supply_info = [
                        'name' => tpCache('basic.store_name'),
                        'logo' => tpCache('basic.store_logo'),
                    ];
                }

                $v['supply_info'] = $supply_info;
                $v['order_can_refund'] = tpCache('shop.canrefund');
                $peisong_order = Db::name('ps_order')
                    ->where('order_id',$v['order_id'])->find();
                $v['peisong_order'] = $peisong_order;
                $v['peisong_status'] = $peisong_order['status']??0;
                $v['ps_order_id'] = $peisong_order['id']??0;
                if($v['refund_id']){
                    $refund_status = Db::name('order_refund')->where('id',$v['refund_id'])->value('status');
                    $v['refund_status'] = $refund_status;
                    $v['refund_status_str'] = $this->refund_status[$refund_status]??'未知';
                }else{
                    $v['refund_status'] = -1;
                    $v['refund_status_str'] = '无';
                }

                $lists->offsetSet($k,$v);
            }
        }

        $shopset = [
            'comment' => tpCache('shop.comment'),
            'canrefund' => tpCache('shop.canrefund'),
        ];
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $lists->items(),
            'canrefund' => 1,
            'shopset' => $shopset
        ];
        return $result;
    }

    public function orderdetail(){
        $id = input('order_id');
        $map = [];
        $map['order_id'] = $id;
        $map['ps_user_id'] = $this->user_id;
        $psorder = Db::name('ps_order')->where($map)->find();
        $map = [];
        $map['order_id'] = $id;
        $map['ps_user_id'] = $this->user_id;
        $order_info = Db::name('order')
            ->where($map)
            ->field('*')
            ->find();
        if (!$order_info) {
            $result = [
                'status' => true,
                'msg' => '没有获取到订单信息',
                'data' => []
            ];
            return $result;
        }
        $leixing_arr = Db::name('goods_leixing')->where('1=1')->column('name','id');
        $order_info['leixing_name'] = $leixing_arr[$order_info['leixing_id']];
        $order_info['add_time'] = date('Y-m-d H:i:s',$order_info['add_time']);
        $order_info['pay_time'] = date('Y-m-d H:i:s',$order_info['pay_time']);
        $order_info['shipping_time'] = date('Y-m-d H:i:s',$order_info['shipping_time']);
        $order_info['confirm_time'] = date('Y-m-d H:i:s',$order_info['confirm_time']);

        $order_info['real_username'] = $order_info['username'];
        $order_info['order_can_refund'] = 1;
        $goods_list = Db::name('order_goods')
            ->alias('og')
            ->join('goods g','og.goods_id=g.goods_id')
            ->where(['order_id'=>$order_info['order_id']])
            ->field('og.rec_id,og.goods_id,og.goods_name,og.goods_num,og.shop_price,og.spec_key_name,g.original_img')
            ->select()->toArray();
        if($goods_list){
            foreach($goods_list as $kk=>$vv){
                $goods_list[$kk]['image'] = $vv['original_img'];
            }
        }
        $order_info['goods_list'] = $goods_list;
        $order_info['procount'] = count($goods_list);
        $province = Db::name('region')->where(array('id'=>$order_info['province']))->value('name');
        $city =  Db::name('region')->where(array('id'=>$order_info['city']))->value('name');
        $district =  Db::name('region')->where(array('id'=>$order_info['district']))->value('name');
        $full_address = $province.' '.$city.' '.$district.' '. $order_info['address'];
        $order_info['full_address'] = $full_address;
        $order_status_arr = logic('OrderLogic')->getStatus();
        $order_info['status_name'] = $order_status_arr[$order_info['order_status']];

        $shopset = [
            'comment' => tpCache('shop.comment'),
            'canrefund' => tpCache('shop.canrefund'),
        ];
        $binfo = Db::name('suppliers')->where('id',$order_info['sid'])->find();
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $order_info,
            'psorder' => $psorder,
            'shopset' => $shopset,
            'binfo' => $binfo
        ];
        return $result;
    }

    //更改取货状态
    public function chuku(){
        $id = input('order_id');
        Db::startTrans();
        $data = [
            'peisong_status' => 1,
            'order_status' => 2,
            'chuku_time' => time(),
        ];
        Db::name('order')->where('order_id',$id)->update($data);
        Db::name('order_goods')->where('order_id',$id)->where('refund_status',0)->update(['order_status'=>2,'peisong_status'=>1]);
        Db::commit();
        $result = [
            'status' => true,
            'msg' => '操作成功！',
            'data' => [],
        ];
        return $result;
    }
    //确认送达
    public function complete(){
        $id = input('order_id');
        $peisong_type = input('peisong_type');
        $peisong_remark = input('peisong_remark');
        $peisong_pics = input('peisong_pics');
        //检查是否有积分，余额支付
        Db::startTrans();
        $data = [
            'order_status' => 3,
            'peisong_type' => $peisong_type,
            'peisong_remark' => $peisong_remark,
            'peisong_pics' => $peisong_pics,
            'confirm_time' => time(),
            'peisong_status' => 2,
        ];
        Db::name('order')->where('order_id',$id)->update($data);
        Db::name('order_goods')->where('order_id',$id)->where('refund_status',0)->update(['order_status'=>3,'peisong_status'=>2]);
        Db::commit();
        $result = [
            'status' => true,
            'msg' => '操作成功！',
            'data' => [],
        ];
        return $result;
    }


    //退款列表
    public function refundlists(){
        $page = input('page');
        //列表过滤器，生成查询Map对象
        $map = [];
        $map['ps_user_id'] = $this->user_id;
        $status = input('st')?:'0';
        //查询订单列表
        $field = 'o.order_id,order_sn,order_status,order_status,total_shop_price,s_user_id,r.status refund_status,r.w_time,r.reason';
        $lists = Db::name('order_refund')
            ->alias('r')
            ->join('order o','r.order_id=o.order_id')
            ->where($map)
            ->order('o.order_id desc')
            ->field($field)
            ->paginate(15,false,['page'=>$page,'list_rows'=>15]);
        if($lists->total()){
            foreach($lists as $k=>$v){
                $goods_list = Db::name('order_goods')
                    ->alias('og')
                    ->join('goods g','og.goods_id=g.goods_id')
                    ->where(['order_id'=>$v['order_id']])
                    ->field('og.goods_id,og.goods_name,og.goods_num,og.shop_price,og.spec_key_name,g.original_img')
                    ->select();
                $num_total = 0;
                if($goods_list){
                    foreach($goods_list as $kk=>$vv){
                        $goods_list[$kk]['image'] = $vv['original_img'];
                        $num_total = bcadd($num_total,$vv['goods_num']);
                    }
                }
                $v['num_total'] = $num_total;
                $v['goods_list'] = $goods_list;
                $supply_info = Db::name('suppliers')->where('id',$v['s_user_id'])->find();
                $v['supply_info'] = $supply_info;
                $lists->offsetSet($k,$v);
            }
        }

        $result = [
            'status' => true,
            'msg' => '',
            'data' => $lists->items(),
        ];
        return $result;
    }
    //退款详情
    public function refund_detail(){
        $id = input('id');
        $map = [];
        $map['r.ps_user_id'] = $this->user_id;
        $map['r.id'] = $id;
        $field = 'o.order_id,order_sn,order_status,order_status,total_shop_price,s_user_id,r.status as refund_status,r.w_time,r.reason';
        $info = Db::name('order_refund')
            ->alias('r')
            ->join('order o','r.order_id=o.order_id')
            ->where($map)
            ->order('o.order_id desc')
            ->field($field)
            ->find();
        $goods_list = Db::name('order_refund_goods')
            ->alias('rog')
            ->join('order_goods og','rog.og_id=og.rec_id')
            ->join('goods g','g.goods_id=og.goods_id')
            ->where(['rog.refund_id'=>$id])
            ->field('rog.goods_id,og.goods_name,rog.num,og.shop_price,og.spec_key_name,g.original_img')
            ->select();
        $info['goods_list'] = $goods_list;
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $info,
        ];
        return $result;
    }

    //退款审核
    public function refund_check(){
        $refund_id = input('refund_id');
        $refund_status = input('refund_status');
        $refund_remark = input('refund_remark');
        $info = Db::name('order_refund')->where('id',$refund_id)->find();
        $data = [];
        $data['status'] = $refund_status;
        $data['remark'] = $refund_remark;
        $data['u_time'] = time();
        Db::name('order_refund')->where('id',$refund_id)->update($data);
        Db::name('order_refund_goods')->where('id',$refund_id)->update($data);
        if($refund_status==1){
            //审核通过，退款
            $order_info = Db::name('order')->where('order_id',$info['order_id'])->find();
        }
        $result = [
            'status' => true,
            'msg' => '操作成功',
        ];
        return $result;
    }

    public function setst(){
        $id = input('id');
        $st = input('st');
        $map = [];
        $map['id'] = $id;
        $map['ps_user_id'] = $this->user_id;
        $psorder = Db::name('ps_order')->where($map)->find();
        Db::startTrans();
        $data_ps = [
            'status' => $st
        ];
        if($st==2){
            $data_ps['endtime'] = time();
        }
        Db::name('ps_order')->where('id',$id)->update($data_ps);
        $data = [];
        if($st==1){
            $data['order_status'] = 2;
        }
        if($st==2){
            $data['order_status'] = 3;
        }
        Db::name('order')->where('order_id',$psorder['order_id'])->update($data);
        Db::commit();
        return [
          'status' => 1,
          'msg' => '操作成功'
        ];
    }

}
